﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;
using ALMDDD.Application.Interfaces;
using ALMDDD.Application.ViewModels.SysUser;
using ALMDDD.Domain.Core.Notifications;
using ALMDDD.Domain.Model;
using MediatR;
using Microsoft.AspNetCore.Mvc;

namespace ALMDDD.Controllers
{
    public class SysUserController : Controller
    {
        private readonly ISysUserService _sysUserService;//用户
        private readonly DomainNotificationHandler _domainNotificationHandler;//消息处理
        public SysUserController(ISysUserService sysUserService,
          INotificationHandler<DomainNotification> notifications)
        {
            _domainNotificationHandler = (DomainNotificationHandler)notifications;
            _sysUserService = sysUserService;
        }
        public IActionResult Index()
        {
            Expression<Func<Sys_User, bool>> exp = c => c.UserId == "admin" && c.PassWord == "111";
            SysUserViewModel list = _sysUserService.GetMany(exp);
            //SysUserViewModel studentViewModel = _sysUserService.GetAll().FirstOrDefault();
            return View(_sysUserService.GetAll());
       
        }
        public ActionResult Create()
        {
            return View();
        }
        [HttpPost]
        public ActionResult Create(SysUserViewModel studentView)
        {

            try
            {
                if (!ModelState.IsValid)
                {
                    return View(studentView);

                }

                _sysUserService.Add(studentView);
                ////对错误进行记录，还需要抛给前台

                var notificacoes = _domainNotificationHandler.GetNotifications();
                notificacoes.ForEach(c => ViewData.ModelState.AddModelError(string.Empty, c.Value));

                return View();
            }
            catch (Exception ex)
            {

                throw;
            }
            //return View(studentView);
        }
    }
}